import {handleError} from "vue";
import * as process from "process";
class Api {
    static baseUri: string = import.meta.env['VITE_API_GATEWAY']
    // static baseUri: string = 'http://octopoda.local:7002';
    static headers: any = {};

    static  POST = 'POST';
    static GET = 'GET';
    static PUT = 'PUT';
   static async send(path: string, data: any = null, method: string = Api.GET, headers: any = {}) {
        const reg = /^(\s|\/)+|(\s|\/)+$/g;
        const baseUrl = Api.baseUri.replace(reg, '');
        path = path.replace(reg, '');
        headers = {
            ...Api.headers,
            ... headers,
            'Content-Type': 'application/json'
        };
       const options: any = {
           headers: headers,
           method: method,
       }
       if (data) {
           options['body'] = JSON.stringify(data);
       }
        let result = await fetch(baseUrl + '/' + path, options);
        let obj = await result.json();
        return obj.status ? obj.data : null;
    }
}

const getVisitor = async (id:string = '') => {
    const path = id ? 'visitor/' + id : 'visitor';
    const res = await Api.send(path);
    return res;
}

const createTopic = async (title: string = '', userId: string = '') => {
    const path = userId ? 'user/' + userId + '/topic': 'user/topic';
    const res = await Api.send(path, {
        user_id: userId,
        title: title
    }, Api.POST);
    return res;
}

const sendAskMessage = async (topicId: string | undefined, message: string, askUserId: string | undefined) => {
    const path = 'topic/'+topicId+'/chat';
    const res = await Api.send(path, {
        topic_id: topicId,
        ask_message: message,
        ask_user_id: askUserId,
        option: 'CHAT'
    }, Api.POST);
    return res;
}

const setTopicTitle = async (topicId: string, title: string) => {
    const path = 'topic/'+topicId;
    const res = await Api.send(path, {
        title: title,
    }, Api.PUT);
    return res;
}
const getTopicMessages = async (topicId: string) => {
    const path = 'user/topic/'+topicId+'/messages';
    const res = await Api.send(path);
    return res;
}

const getTopicDetail = async (topicId: string) => {
    const path = 'user/topic/'+topicId;
    const res = await Api.send(path);
    return res;
}

const getTopicList = async (userId: string) => {
    const path = 'user/'+userId + '/topics';
    const res = await Api.send(path);
    return res;
}

const addCollection =async (title: string = '', userId: string = '', collection: Object) => {
    const path = userId ? 'user/'+userId+'/collection' : 'user/collection';
    const res =await Api.send(path, {
        user_id: userId,
        title: title,
        collection: collection
    }, Api.POST);
    return res;
}

const getCollections = async (userId: string = '') => {
    const path = 'user/'+userId+'/collections';
    const res = await Api.send(path);
    return res;
}

const getCollectionMessages =async (collectionId: string = '') => {
    const path = 'collection/'+collectionId+'/messages';
    const res = await Api.send(path);
    return res;
}

const edit = async (message: string,  messageId: string, opt: string, option: any = {}) => {
    const path = 'message/' + messageId + '/edit';
    const res = await Api.send(path, {
        message: message,
        opt_option: option,
        message_id: messageId,
        opt_mode: opt
    }, Api.POST);
    return res;
}

const reAnswerQa = async (qaId: string, message: string = '') => {
    const path = 'qa/' + qaId + '/reanswer';
    const res = await Api.send(path, {
        message: message,
    }, Api.PUT);
    return res;
}


export {getVisitor, createTopic, sendAskMessage, setTopicTitle, getTopicMessages, edit, reAnswerQa,
    getTopicDetail, getTopicList, addCollection, getCollections, getCollectionMessages};